﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using SmartSpace.Core;
using System.Data.Entity.Migrations;

namespace WorkSpace.Core
{
    public class WorkSpaceDbContext : DbContext
    {
        public WorkSpaceDbContext()
            : base("name=" + SmartSpace.Configuration.DefaultConnectionStringName)
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<WorkSpaceDbContext>(new MigrateDatabaseToLatestVersion<WorkSpaceDbContext, DBMC>());
            }
            else
            {
                Database.SetInitializer<CoreDbContext>(null);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            WorkSpaceContext.Build(modelBuilder);

            base.OnModelCreating(modelBuilder);
        }
    }

    internal sealed class DBMC : DbMigrationsConfiguration<WorkSpaceDbContext>
    {
        public DBMC()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}
